﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using FAP.Cores.CaptchaCenter;
using log4net;

namespace FAP.Cores.Task
{
    public class CaptchaTask : FapTask
    {
        public CaptchaDTO Captcha;
        private ILog m_log = LogManager.GetLogger(typeof(FapTask));
        private CaptchaTask() { }
        public CaptchaTask(CaptchaDTO captcha)
        {
            this.Captcha = captcha;
            this.TaskName = "Captcha";
        }

        public void DoWork()
        {
            m_log.Info(string.Format("Working task {0}", TaskIndex));
            _iStatus = 1;
            if (onChangeStatus != null)
                this.onChangeStatus(this, TaskIndex);
            object result = null;
            try
            {
                result = this.RunIt();
                if (onFinished != null)
                {
                    m_log.Info(string.Format("Finished task {0}", TaskIndex));
                    //this._iStatus = 2;
                    if (onChangeStatus != null)
                        this.onChangeStatus(this, Status);
                    //this.onFinished(this, result);
                    FAP.Cores.Threading.ThreadControl.m_lstTask.Remove(this);
                    m_log.Info(string.Format("Removed task {0}", TaskIndex));
                }
            }
            catch (Exception ex)
            {
                if (onError != null)
                {
                    m_log.Error(string.Format("Error task {0}", TaskIndex), ex);
                    _iStatus = 3;
                    if (onChangeStatus != null)
                        this.onChangeStatus(this, Status);
                    this.onError(this, ex);
                    FAP.Cores.Threading.ThreadControl.m_lstTask.Remove(this);
                    m_log.Info(string.Format("Removed task {0}", TaskIndex));
                }
            }
        }

        public override object RunIt()
        {
            CaptchaManager.GetCaptcha(this);
            return null;
        }
    }
}
